$(function () {
    // 等待加载
    loading();
    $("#add").click(function () {
        var name = $("#name").val();
        var cookie = new sweetCookie();
        cookie.setCookie("company_name", name);
        window.location.href = '/Lvscore_User_M/userWeb/views/myAD/myContenter/searchContenter.html';
    })
    ajaxmyDevelopers(1);
    $(".liBox li a").click(function () {
        // 等待加载
        $("#myContener").empty();
        loading();
        $(this).each(function () {
            $(".liBox li a").prop("class", "");
            $(this).prop("class", "cur");
            var text = $(this).prop("text");
            if (text == 'All'){
                ajaxmyDevelopers(1);

            }else{
                findByFl(text);
            }

        })
    })
});

function findByFl(f) {

    ajaxfindmyDevelopers(null, 1, f);
}


/**
 * 按首字母A-Z查询我的内容商
 * @param page    当前页码（需要加载哪一页的数据）
 * @param first    首字母A-Z
 */

function ajaxfindmyDevelopers(info, page, first) {
    var url = "/Lvscore_User_M/myAds/ajaxsearchDevelopers";
    $.post(url, {
        info: info,
        page: page,
        first: first

    }, function (data) {
        // sumpage：页容量（页面的数据条数）
        // nowpage：页码
        // developersList：List<DeveloperBean>
        // id:内容商id
        // d.donkey_num:小毛驴id
        // d.company_name:公司名称
        // d.company_address:公司地址
        $("#myContener").empty();
        if (data.nowpage == 1) {
            $(".tcdPageCode").empty().unbind();
        }
        if (data.developersList.length == 0) {
            $("#myContener").html('<p class="noResult"><img src="/Lvscore_User_M/userWeb/static/images/dissearch.png" alt="">未查询到任何数据</p>');

        } else {
            $.each(data.developersList, function (index, element) {
            	if(!isNotEmpty(element.logo)){
    				element.logo="/Lvscore_User_M/userWeb/static/images/manageWrap_pic.png";
    	    	}
            	if(element.company_address==null || element.company_address==""){
            		element.company_address="无";
            	}
                $("#myContener").append("<dl class='person'>" +
                    "<dt><img src='"+element.logo+"' width='90px' onclick='toAddedContenterDetail(" + element.id + ")'></dt>" +
                    "<dd>" +
                    "<p><a href='javascript:toAddedContenterDetail(" + element.id + ")'><span>" + element.company_name + "</span></a></p>" +
                    "<p>" + element.donkey_num + "</p>" +
                    "<p>" + element.company_address + "</p>" +
                    "<a href='javascript:void(0)'><input type='button' value='取消关注' class='delete' onclick='ajaxDeleteDeveloper(" + element.id + ");'></a>" +
                    "</dd>" +
                    "</dl>" );
            });
            if ($(".tcdPageCode").html() == "") {
                createPageFind(data.sumpage, data.nowpage, first, info);
            }
        }
        //等待加载的效果消失
        $(".loadingBox").hide();
    }, "json");
}


/**
 * 加载我的内容商列表
 * @param page 当前页码（为空时显示第一页）
 */
function ajaxmyDevelopers(page) {
    var url = "/Lvscore_User_M/myAds/ajaxmyDevelopers";
    $.post(url, {
            page: page
        }, function (data) {
            $("#myContener").empty();
            if (data.developersList.length == 0) {
                $("#myContener").html('<p class="noResult"><img src="/Lvscore_User_M/userWeb/static/images/dissearch.png" alt="">您暂时没有添加任何内容商</p>');
            } else {
                $.each(data.developersList, function (index, element) {
                	if(!isNotEmpty(element.logo)){
                		element.logo="/Lvscore_User_M/userWeb/static/images/manageWrap_pic.png";
                	};
                	if(element.company_address==null || element.company_address==""){
                		element.company_address="无";
                	};
                    $("#myContener").append("<dl class='person'>" +
                        "<dt><img class='handes' src='"+element.logo+"' width='90px' onclick='toAddedContenterDetail(" + element.id + ")'></dt>" +
                        "<dd>" +
                        "<p><a href='javascript:toAddedContenterDetail(" + element.id + ")'><span>" + element.company_name + "</span></a></p>" +
                        "<p>" + element.donkey_num + "</p>" +
                        "<p>" + element.company_address + "</p>" +
                        "<a href='javascript:void(0)'><input type='button' value='取消关注' class='delete' onclick='confirmBox(" + element.id + ")'></a>" +
                        "</dd>" +
                        "</dl>");

                });
                if ($(".tcdPageCode").html() == "") {
                    createPage(data.sumpage, data.nowpage);
                }
            }

        //等待加载的效果消失
        $(".loadingBox").hide();

        }, "json"
    );
}

/**
 * 确认框
 * @param contenterID
 * @returns
 */
function confirmBox(contenterID){
    $(".mask").show();
    $(".confirmBox").show();
    $(".unCare").click(function(){
        $(".confirmBox").hide();
        ajaxDeleteDeveloper(contenterID);
    })
}

/**
 * 根据获取的值跳转页面
 * @returns
 */
function getName() {
    var name = $("#name").val();
    var json = JSON.stringify(name);
    var cookie = new sweetCookie();
    cookie.set("company_name", json);
    window.location.href = '/Lvscore_User_M/userWeb/views/myAD/myContenter/searchContenter.html';
}

/**
 * 跳转内容商详情页面
 * @param id内容商ID
 * @returns
 */
function toAddedContenterDetail(id) {
    window.location.href = "/Lvscore_User_M/userWeb/views/myAD/myContenter/AddedContenterDetail.html?devId=" + id;
}

function createPage(pages, pageNum) {
    if (pages > 1) {
        $(".tcdPageCode").createPage({
            pageCount: pages,
            current: pageNum,
            backFn: function (p) {
                ajaxmyDevelopers(p);
            }
        });
    }
}
/**
 * 按首字母查找分页
 * @param pages
 * @param pageNum
 * @param f
 * @param info
 * @returns
 */
function createPageFind(pages, pageNum, f, info) {
    if (pages > 1) {
        $(".tcdPageCode").createPage({
            pageCount: pages,
            current: pageNum,
            backFn: function (p) {
                ajaxfindmyDevelopers(info, p, f);
            }
        });
    }
}	